package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.tbit.uqbike.object.pojo.vo.VehicleOperationReportVO;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.webmanager.business.vehicle_operation_report.service.VehicleOperationReportService;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtils;
import com.tbit.utils.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 车辆运营报表
 *
 * @Author:BUGTian
 * @DATE: 2022/11/10
 */
@Slf4j
@RequestMapping("/vehicleOperationReport")
@RestController
public class VehicleOperationReportController {

    @Autowired
    private VehicleOperationReportService vehicleOperationReportService;

    /**
     * 查询车辆运营报表
     *
     * @param adAccountIds 区域id列表，逗号分隔
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @param timeA        自定义查询低电车辆占比A
     * @param timeB        自定义查询低电车辆占比B
     * @return
     */
    @GetMapping("/queryByDay")
    public Result<List<VehicleOperationReportVO>> queryByDay(String adAccountIds, Date startTime, Date endTime, String timeA, String timeB) {
        Assert.notNulls("参数有误", adAccountIds, startTime, endTime, timeA, timeB);
        List<Integer> adAccountIdList = Arrays.stream(adAccountIds.split(","))
                .map(Integer::valueOf)
                .collect(Collectors.toList());
        List<VehicleOperationReportVO> list = vehicleOperationReportService.queryByDay(adAccountIdList, startTime, endTime, timeA, timeB);
        for (VehicleOperationReportVO vehicleOperationReportVO : list) {
            String rideMeterAvgPerCar = vehicleOperationReportVO.getRideMeterAvgPerCar();
            if (StrUtil.isNotEmpty(rideMeterAvgPerCar)){
                Double div = NumberUtil.div(Double.parseDouble(rideMeterAvgPerCar), 1000D, 3);
                vehicleOperationReportVO.setRideMeterAvgPerCar(div.toString());
            }
        }
        return Result.success(list);
    }

    /**
     * 导出车辆运营报表
     *
     * @param adAccountIds 区域id列表，逗号分隔
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @param timeA        自定义查询低电车辆占比A
     * @param timeB        自定义查询低电车辆占比B
     * @return
     */
    @GetMapping("/exportByDay")
    public void exportByDay(String adAccountIds, Date startTime, Date endTime, String timeA, String timeB, HttpServletResponse response) {
        Assert.notNulls("参数有误", adAccountIds, startTime, endTime, timeA, timeB);
        List<Integer> adAccountIdList = Arrays.stream(adAccountIds.split(","))
                .map(Integer::valueOf)
                .collect(Collectors.toList());
        List<VehicleOperationReportVO> list = vehicleOperationReportService.queryByDay(adAccountIdList, startTime, endTime, timeA, timeB);
        for (VehicleOperationReportVO vehicleOperationReportVO : list) {
            String rideMeterAvgPerCar = vehicleOperationReportVO.getRideMeterAvgPerCar();
            if (StrUtil.isNotEmpty(rideMeterAvgPerCar)){
                Double div = NumberUtil.div(Double.parseDouble(rideMeterAvgPerCar), 1000D, 3);
                vehicleOperationReportVO.setRideMeterAvgPerCar(div.toString());
            }
        }
        LinkedHashMap<String, String> titleMap = new LinkedHashMap<>();
        titleMap.put("day", "日期");
        titleMap.put("accountName", "运营区");
        titleMap.put("putRate", "车辆投放率");
        titleMap.put("shelvesRate", "车辆上架率");
        titleMap.put("rentRate", "车辆出租率");
        titleMap.put("profitRate", "骑行盈利率");
        titleMap.put("incomeAvgPerCar", "车均收入");
        titleMap.put("orderAvgPerCar", "车均订单数");
        titleMap.put("rideMinuteAvgPerCar", "车均骑行时长");
        titleMap.put("rideMeterAvgPerCar", "车均骑行里程（KM）");
        titleMap.put("maintainRate", "维修完成率");
        titleMap.put("dispatchRate", "调度有效率");
        titleMap.put("moveRate", "挪车完成率");
        titleMap.put("lowBatteryAvg", "低电平均值");
        titleMap.put("lowRateA", String.format("低电车辆占比(%s)", timeA));
        titleMap.put("lowRateB", String.format("低电车辆占比(%s)", timeB));
        try {
            ExcelUtils.listToExcel(list, titleMap, "车辆运营报表", response);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }


}
